package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/users")
@Slf4j
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    /**
     * 查询用户列表
     */
    @GetMapping
    public Result list(UserDto userDto){
        log.info("查询用户,参数:{}",userDto);
        PageResult<User> pageResult =userService.list(userDto);
        return Result.success(pageResult);
    }
    /**
     * 添加用户
     */
    @PostMapping
    public Result add(@RequestBody User user){
        log.info("添加参数{}",user);
        userService.add(user);
        return Result.success();

    }
    /**
     * 删除用户
     */
    @DeleteMapping("/{ids}")
    public Result deleteByIds(@PathVariable List<Integer> ids){
        log.info("删除参数{}",ids);
        userService.deleteByIds(ids);
        return Result.success();
    }
    /**
     * 根据id查询用户
     */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer  id){
        log.info("查询用户id:{}", id);
        User user =userService.selectById(id);
        return Result.success(user);
    }
    /**
     * 修改用户
     */
    @PutMapping
    public Result updateById(@RequestBody User  user){
        log.info("修改用户:{}", user);
        userService.update(user);
        return Result.success();
    }

    /**
     * 查询所有用户
     */
    @GetMapping("/list")
    public Result selectAll(){
        List<User> userList = userService.selectAll();

        return Result.success(userList);
    }
    /**
     * 根据角色查询
     */
    @GetMapping("/role/{roleLabel}")
    public Result selectByRole(@PathVariable String roleLabel){
        log.info("参数：{}",roleLabel);
        List<User> userList = userService.selectByRole(roleLabel);
        return Result.success(userList);
    }
    /**
     * 根据部门查询
     */
    @GetMapping("/dept/{deptId}")
    public Result selectByDeptId(@PathVariable Integer deptId){
        log.info("deptID:{}",deptId);
        List<User> userList = userService.selectByDeptId(deptId);

        return Result.success(userList);
    }

}
